
package org.abbett.eskine.controller;

import org.abbett.eskine.model.CustomIdentity;
import org.abbett.eskine.entity.User;
import org.abbett.eskine.dao.UserDao;
import org.abbett.eskine.entity.Page;
import org.abbett.eskine.dao.PageDao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;


import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.event.ActionEvent;
import java.util.Map;
import java.util.List;

import org.abbett.eskine.service.DebugService;


/**
 * Glue code to bind Application Debug Service with the view framework (Seam). This class contains
 * a default implementation of the action methods. Please avoid reimplmenting the action methods in this class.
 * The purpose of this class is to declare properties which can be bound to the UI.
 * 
  * Displays all entities currently stored in the application.
  *
 * Automatically generated by clickframes
 * 
 * @author Vineet Manohar
 */
public abstract class GeneratedDebugController extends Controller {
 
    @In("#{userDao}") 
    protected UserDao userDao;

    @In("#{pageDao}") 
    protected PageDao pageDao;


    /** the User row which on which the action was performed is stored on 
     * this field via the corresponding action listener if a user performs an action on the 
     * displayed User list 
     */

    /** Selected Users row */
    private User usersSelection;

    /** 
     * Get Selected Users
     *  
     * @return Selected Users
     */
    public User getUsersSelection() {
       return this.usersSelection;
    }

    /**
     * Set Selected Users
     *
     * @param usersSelection
     */
    public void setUsersSelection(User usersSelection) {
        this.usersSelection = usersSelection;
    }


    public void usersListener(ActionEvent actionEvent) {
        UIComponent tmpComponent = actionEvent.getComponent();

        while (null != tmpComponent && !(tmpComponent instanceof UIData)) {
            tmpComponent = tmpComponent.getParent();
        }

        UIData uiData = (UIData) tmpComponent;
        Object selectedRow = uiData.getRowData();
        if (selectedRow instanceof User) {
            this.usersSelection = (User) selectedRow;
        }
    }


    /** 
        				All users.
        			 */
    private List<User> users;

    /** 
     * Get All Users
     *  
     * @return All Users
     */
    public List<User> getUsers() {
       return this.users;
    }

    /**
     * Set All Users
     *
     * @param users
     */
    public void setUsers(List<User> users) {
        this.users = users;
    }


    /** the Page row which on which the action was performed is stored on 
     * this field via the corresponding action listener if a user performs an action on the 
     * displayed Page list 
     */

    /** Selected Pages row */
    private Page pagesSelection;

    /** 
     * Get Selected Pages
     *  
     * @return Selected Pages
     */
    public Page getPagesSelection() {
       return this.pagesSelection;
    }

    /**
     * Set Selected Pages
     *
     * @param pagesSelection
     */
    public void setPagesSelection(Page pagesSelection) {
        this.pagesSelection = pagesSelection;
    }


    public void pagesListener(ActionEvent actionEvent) {
        UIComponent tmpComponent = actionEvent.getComponent();

        while (null != tmpComponent && !(tmpComponent instanceof UIData)) {
            tmpComponent = tmpComponent.getParent();
        }

        UIData uiData = (UIData) tmpComponent;
        Object selectedRow = uiData.getRowData();
        if (selectedRow instanceof Page) {
            this.pagesSelection = (Page) selectedRow;
        }
    }


    /** 
        				All pages.
        			 */
    private List<Page> pages;

    /** 
     * Get All Pages
     *  
     * @return All Pages
     */
    public List<Page> getPages() {
       return this.pages;
    }

    /**
     * Set All Pages
     *
     * @param pages
     */
    public void setPages(List<Page> pages) {
        this.pages = pages;
    }


    
    @In("#{debugPageService}")
	protected DebugService debugService;

	@SuppressWarnings("unused")
	private static final Log logger = LogFactory.getLog(GeneratedDebugController.class);

	@In
	protected CustomIdentity identity;

    @Create
    public void init() {
         // all inputs initialized here 
    }



    protected void initUsers() {
        // if users has not been initialized, initialize it now
        if (getUsers() == null) {
            setUsers(loadUsers());
        }
    }

    /**
     * clears the current value and loads the value again
     */
    protected void reloadUsers() {
        clearUsers();
        loadUsers();
    }

    /**
     * clears the current value of All Users 
     */
    protected void clearUsers() {
        setUsers(null);
    }

    protected List<org.abbett.eskine.entity.User> loadUsers() {
        return userDao.findUsers(describe());
    }

    protected void initPages() {
        // if pages has not been initialized, initialize it now
        if (getPages() == null) {
            setPages(loadPages());
        }
    }

    /**
     * clears the current value and loads the value again
     */
    protected void reloadPages() {
        clearPages();
        loadPages();
    }

    /**
     * clears the current value of All Pages 
     */
    protected void clearPages() {
        setPages(null);
    }

    protected List<org.abbett.eskine.entity.Page> loadPages() {
        return pageDao.findPages(describe());
    }


	/**
	 * This method should be called before the page is loaded
	 * 
	 * All the page parameters should be consumed in this method
	 */
    public void processPageParameters() {

        initUsers();

        initPages();


          // if you need to add more code, override this method in the child class 
    }



 


    private static final long serialVersionUID = 1L;
}// clickframes::version=4055003532::clickframes